const app = getApp();
Page({

  data: {
    id:'',
    education:'',
    school:'',
    speciality:'',
    start_time:'请选择入学日期',
    end_time:'请选择毕业日期',
    diploma_pics:[],
    degree_pics:[],
    click_button:0
  },

  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id
    })
    that.getSchoolInfo();
  },

  onShow: function () {
    let that = this;
  },

  get_education: function(e){
    var that=this;
    that.setData({
      education:e.detail.value
    })
  },

  get_school: function(e){
    var that=this;
    that.setData({
      school:e.detail.value
    })
  },

  get_speciality: function(e){
    var that=this;
    that.setData({
      speciality:e.detail.value
    })
  },

  bindStartChange: function(e) {
    this.setData({
      start_time: e.detail.value
    })
  },

  bindEndChange: function(e) {
    this.setData({
      end_time: e.detail.value
    })
  },

  getSchoolInfo: function(e) {
    var that = this;
    wx.request({
      url: app.globalData.domain + 'index/getSchoolInfo',
      method: "POST",
      data: {
        id: that.data.id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: (res) => {
        that.setData({
          education: res.data.info.education,
          school: res.data.info.school,
          speciality: res.data.info.speciality,
          start_time: res.data.info.start_time,
          end_time: res.data.info.end_time,
          diploma_pics: [res.data.info.diploma],
          degree_pics: [res.data.info.degree]
        })
      },
    })
  },

  updataDiplomaImg: function(e) {
    var that = this;
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        if (res.tempFilePaths) {
          var counts=res.tempFilePaths.length + that.data.diploma_pics.length;
          if(counts>1){
            wx.showToast({
              title: '最多上传1张图片',
              icon: 'none',
              duration: 2000,
            })
          }else{
            for (var i = 0; i < res.tempFilePaths.length; i++) {
              wx.uploadFile({
                url: app.globalData.domain + 'index/uploadImages',
                filePath: res.tempFilePaths[i],
                name: 'file',
                success: function(r) {
                  that.data.diploma_pics.push(r.data);
                  that.setData({
                    diploma_pics: that.data.diploma_pics
                  });
                }
              })
            }
          }
        }
      }
    })
  },

  delectDiplomaImg: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.diploma_pics.splice(index, 1);
    that.setData({
      diploma_pics: that.data.diploma_pics
    })
  },

  updataDegreeImg: function(e) {
    var that = this;
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        if (res.tempFilePaths) {
          var counts=res.tempFilePaths.length + that.data.degree_pics.length;
          if(counts>1){
            wx.showToast({
              title: '最多上传1张图片',
              icon: 'none',
              duration: 2000,
            })
          }else{
            for (var i = 0; i < res.tempFilePaths.length; i++) {
              wx.uploadFile({
                url: app.globalData.domain + 'index/uploadImages',
                filePath: res.tempFilePaths[i],
                name: 'file',
                success: function(r) {
                  that.data.degree_pics.push(r.data);
                  that.setData({
                    degree_pics: that.data.degree_pics
                  });
                }
              })
            }
          }
        }
      }
    })
  },

  delectDegreeImg: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.degree_pics.splice(index, 1);
    that.setData({
      degree_pics: that.data.degree_pics
    })
  },

  big_preview: function (e) {
    var url = e.currentTarget.dataset.url;
    var urls = e.currentTarget.dataset.urls;
    var previewImgArr = urls;
    wx.previewImage({
      current: url,
      urls: previewImgArr
    })
  },

  submit_result:function(){
    var that = this;
    if (that.data.education == '') {
      wx.showToast({
        title: '请输入学历',
        icon: 'none',
        duration: 2000,
      })
    } else if (that.data.school  == '') {
      wx.showToast({
        title: '请输入学校',
        icon: 'none',
        duration: 2000,
      })
    } else if (that.data.speciality=='') {
      wx.showToast({
        title: '请输入专业',
        icon: 'none',
        duration: 2000,
      })
    } else if (that.data.start_time=='请选择入学日期') {
      wx.showToast({
        title: '请选择入学日期',
        icon: 'none',
        duration: 2000,
      })
    } else if (that.data.end_time=='请选择毕业日期') {
      wx.showToast({
        title: '请选择毕业日期',
        icon: 'none',
        duration: 2000,
      })
    } else if (that.data.diploma_pics.length==0) {
      wx.showToast({
        title: '请上传毕业证',
        icon: 'none',
        duration: 2000,
      })
    } else if (that.data.degree_pics.length==0) {
      wx.showToast({
        title: '请上传学位证',
        icon: 'none',
        duration: 2000,
      })
    }  else {
      this.setData({
        click_button: 1
      })
      wx.request({
        url: app.globalData.domain + 'index/updateSchool',
        method: "POST",
        data: {
          id: that.data.id,
          education: that.data.education,
          school: that.data.school,
          speciality: that.data.speciality,
          start_time: that.data.start_time,
          end_time: that.data.end_time,
          diploma_pics: that.data.diploma_pics,
          degree_pics: that.data.degree_pics,
          user_id: wx.getStorageSync('userinfo').id
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function(res) {
          if (res.data.code == 0) {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000,
              success: function() {
                that.setData({
                  click_button:0
                })
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1
                  });
                }, 1000);
              }
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000,
              success: function() {}
            })
          }
        }
      })
    }
  },

})